package worker;
import java.util.Comparator;
import common.JobObject;

public class SortByPriority implements Comparator<JobObject> {
	
	final static String ACS = "ACS";
	final static String DECS = "DECS";

	private String state;

	public SortByPriority(String state) {
		this.state = state;
	}

	public SortByPriority() {

	}

	public int compare(JobObject o1, JobObject o2) {
		if (state.equals(DECS)) {
			return descendingSort(o1, o2);
		} else if (state.equals(ACS)) {
			return ascendingSort(o1, o2);
		} else {
			return 0;
		}
	}

	// sort the object in descending way
	private int descendingSort(JobObject o1, JobObject o2) {
		if (o1.getPriority() < o2.getPriority()) {
			return 1;
		} else if (o1.getPriority() > o2.getPriority()) {
			return -1;
		} else
			return 0;
	}

	// sort the object in ascending way
	private int ascendingSort(JobObject o1, JobObject o2) {
		if (o1.getPriority() > o2.getPriority()) {
			return 1;
		} else if (o1.getPriority() < o2.getPriority()) {
			return -1;
		} else
			return 0;
	}
}
